home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / system / ced_adds.zip / PUSHDIR.ASM < prev    next >
Assembly Source File  |  1995-10-30  |  7KB  |  276 lines

  1.     ;    PUSHD/POPD    Ver. 1.0
  2.     ;
  3.     ;    PUSHD saves the current directory on a stack. With the command
  4.     ;    POPD you change automatically back to that saved directory.
  5.     ;    The program, once loaded, stays in RAM and works as an
  6.     ;    extension of the command interpreter.
  7.     ;
  8.     ;    Copyright (C) 1987 by Urs Zurbuchen, Switzerland
  9.     ;
  10.     ;        You may freely distribute this program unless you
  11.     ;        include the sources and don't touch this header.
  12.     ;        Commercial usage is prohibited.
  13.     ;
  14.     ;    Requirements:
  15.     ;        This program requires that the command editor CED
  16.     ;        (by C.J. Dunford) is installed.
  17.     ;
  18.     ;    Restrictions:
  19.     ;        The directory stack has enough room for 5 entries.
  20.     ;        Each entry uses 64 bytes of RAM for storage.
  21.     ;
  22.     ;    Usage:        PUSHD
  23.     ;            POPD
  24.     ;
  25.     ;    Assembly:    MASM pushd;        MASM popd;
  26.     ;            LINK pushd;        LINK popd;
  27.     ;            EXE2BIN pushd pushd.com    EXE2BIN popd popd.com
  28.     ;
  29.     ;    Author:        Urs Zurbuchen
  30.     ;
  31.     ;    Date:        February 9, 1987
  32.     ;
  33.     ;    Revisions:
  34.     ;        Date    Who        What
  35.     ;
  36.     ;----------------------------------------------------------------------
  37.  
  38.     ; adjust the following global according to the processor used
  39. is_8088    equ    0            ; you won't have those fancy '286
  40.                     ; instructions if true
  41.     if    is_8088
  42.         .8086
  43.     else
  44.         .286c            ; this requires MASM 4.0
  45.     endif
  46.  
  47.  
  48.     ; some global constants
  49. cr    equ    13            ; CarriageReturn
  50. lf    equ    10            ; LineFeed
  51. space    equ    32            ; (what do you guess :-)
  52. tab    equ     9            ; Tabulator
  53.  
  54.     ; definitions for the CED interface
  55. ced    equ    0ffh            ; special DOS function for CED services
  56. enqueue    equ    0            ; Subfunction to CED: enqueue command
  57. dequeue    equ    1            ; Subfunction to CED: dequeue command
  58. ced_dos    equ    1            ; CED parameter: command at DOS prompt
  59. ced_usr    equ    2            ; CED parameter: command in user prg
  60.  
  61.     ; configurable constants
  62. entries    equ    5            ; max. number of directories to push
  63.  
  64.     ; some program specific constants   DON'T TOUCH THEM !!!!
  65. plength    equ    65            ; pathname-buffer length (one entry)
  66. p2    equ    6
  67.  
  68.     ; here follows the program section
  69. cseg    segment    para public 'code'
  70.     assume    cs:cseg, ds:cseg, es:cseg
  71.  
  72.     ; initialization
  73.     org    100h
  74.  
  75.     ;-----------------------
  76.     ; Installation procedure
  77.     ;-----------------------
  78. main    proc    far
  79.     jmp    install            ; go to installation
  80.  
  81.     ; datastructures
  82. pbuf    db    entries * plength dup (?) ; pathname buffer
  83. pnum    dw    0            ; "stack pointer"
  84.  
  85.     ; error messages
  86. msg1$    db    'pushd/popd: no parameters allowed', cr, lf, '$'
  87. msg2$    db    'pushd: stack full', cr, lf, '$'
  88. msg3$    db    'popd: stack empty', cr, lf, '$'
  89. msg4$    db    'pushd: cannot get current directory', cr, lf, '$'
  90. msg5$    db    'popd: cannot change to saved directory', cr, lf, '$'
  91.  
  92.     ;--------------------
  93.     ; the PUSHD procedure
  94.     ;--------------------
  95. pushd    proc    far
  96.     mov    byte ptr [si],cr    ; null out the users input
  97.     mov    bp,dx
  98.  
  99. psh_1:    mov    al,byte ptr es:[bp]    ; test for parameters
  100.     cmp    al,cr            ;   end of input reached ?
  101.     jz    psh_ok
  102.     inc    bp
  103.     cmp    al,space        ;   space and tab are ok at the tail
  104.     jz    psh_1
  105.     cmp    al,tab
  106.     jz    psh_1
  107.     jmp    err_noparam        ;   any other char is bad enough
  108.  
  109. psh_ok:    push    cs            ; set data segment context
  110.     pop    ds
  111.  
  112.     mov    ax,word ptr [pnum]    ; is there any room left
  113.     cmp    ax,5
  114.     jz    err_noroom        ;   no: tell the user
  115.  
  116.                     ; prepare for DOS call
  117.     mov    si,ax             ;   make room for first '\'
  118.     if    is_8088
  119.     mov    cl,p2            ;   adjust for the different processors
  120.     sal    ax,cl
  121.     else
  122.     sal    ax,p2
  123.     endif
  124.     add    ax,offset pbuf        ;   compute buffer adress
  125.     add    si,ax            ;   DS:SI points to 65-byte buffer area
  126.     inc    si
  127.     mov    dl,0            ;   indicates current drive
  128.  
  129.     push    si
  130.     mov    ah,47h            ; get current directory
  131.     int    21h
  132.     pop    si
  133.     jc    err_fnerr1        ; tell user if we got an error
  134.  
  135.     inc    word ptr [pnum]        ; increment "stack pointer"
  136.     mov    byte ptr [si-1],'\'    ; patch pathname
  137.  
  138.     ret                ; exit to CED
  139.  
  140. pushd    endp
  141.  
  142.     ;-------------------
  143.     ; the POPD procedure
  144.     ;-------------------
  145. popd    proc    far
  146.     mov    byte ptr [si],cr    ; null out the users input
  147.     mov    bp,dx
  148.  
  149. pop_1:    mov    al,byte ptr es:[bp]    ; test for parameters
  150.     cmp    al,cr            ;   end of input reached ?
  151.     jz    pop_ok
  152.     inc    bp
  153.     cmp    al,space        ;   space and tab are ok at the tail
  154.     jz    pop_1
  155.     cmp    al,tab
  156.     jz    pop_1
  157.     jmp    err_noparam        ;   any other char is bad enough
  158.  
  159. pop_ok:    push    cs            ; set data segment context
  160.     pop    ds
  161.  
  162.     mov    ax,word ptr [pnum]    ; is there any pathname on the stack
  163.     or    ax,ax
  164.     jz    err_empty        ;   no: tell the user
  165.  
  166.     dec    ax            ; prepare for DOS call
  167.     mov    dx,ax
  168.     if    is_8088
  169.     mov    cl,p2            ;   adjust for the different processors
  170.     sal    ax,cl
  171.     else
  172.     sal    ax,p2
  173.     endif
  174.     add    ax,offset pbuf        ;   compute buffer adress
  175.     add    dx,ax            ;   DS:DX points to 65-byte buffer area
  176.  
  177.     mov    ah,3bh            ; change current directory
  178.     int    21h
  179.     jc    err_fnerr2        ; tell user if we got an error
  180.  
  181.     dec    word ptr [pnum]        ; decrement "stack pointer"
  182. pop_end:
  183.     ret                ; exit to CED
  184. popd    endp
  185.  
  186.  
  187.     ;--------------
  188.     ; error handler
  189.     ;--------------
  190. error    proc    near
  191. err_noparam:                ; no parameters allowed
  192.     push    cs
  193.     pop    ds
  194.     mov    dx,offset msg1$
  195.     jmp    err_common
  196.  
  197. err_noroom:                ; no more room for push
  198.     mov    dx,offset msg2$
  199.     jmp    err_common
  200.  
  201. err_empty:                ; stack is empty
  202.     mov    dx,offset msg3$
  203.     jmp    err_common
  204.  
  205. err_fnerr1:                ; error in getting current directory
  206.     mov    dx,offset msg4$
  207.     jmp    err_common
  208.  
  209. err_fnerr2:                ; error in changing to new directory
  210.     mov    dx,offset msg5$
  211.     jmp    err_common
  212.  
  213. err_common:                ; common point for error output
  214.     mov    ah,9            ; print string
  215.     int    21h
  216.     jmp    pop_end
  217.  
  218. error    endp
  219.  
  220.     ;-------------------------------------------------------------------
  221.     ; This is the installation routine. It queues the two commands above
  222.     ; into the CED command editor. It terminates but leaves the command
  223.     ; routines resident.
  224.     ;-------------------------------------------------------------------
  225. install:
  226.     push    cs
  227.     pop    ds
  228.     push    ds
  229.     pop    es
  230.  
  231.     mov    ah,ced            ; request CED service from DOS
  232.     mov    al,enqueue        ; enqueue a new command
  233.     mov    bl,ced_dos        ; PUSHD is only active at DOS prompt
  234.     mov    si,offset pushd$    ; pointer to command name
  235.     mov    di,offset pushd        ; pointer to service routine
  236.     int    21h
  237.     jnc    inst_next        ; skip error handler
  238.  
  239.     mov    dx,offset msg_inst1$    ; only error 8 is possible here
  240. inst_err:
  241.     mov    ah,9            ; print string
  242.     int    21h
  243.     int    20h            ; exit without staying resident
  244.  
  245. inst_next:
  246.     mov    ah,ced            ; request CED service once more
  247.     mov    al,enqueue        ; enqueue a new command
  248.     mov    bl,ced_dos        ; POPD is only active at the DOS prompt
  249.     mov    si,offset popd$        ; pointer to command name
  250.     mov    di,offset popd        ; pointer to service routine
  251.     int    21h
  252.     jnc    inst_ok            ; no error: exit
  253.  
  254.     mov    ah,ced            ; dequeue pushd if we can't
  255.     mov    al,dequeue        ;   install popd
  256.     mov    si,offset popd$        ; pointer to command name
  257.     int    21h            ; no error possible for dequeue
  258.     mov    dx,offset msg_inst2$    ; now, tell the user about the error
  259.     jmp    inst_err
  260.  
  261. inst_ok:
  262.     mov    dx,offset install    ; ok: terminate/resident
  263.     int    27h
  264. main    endp
  265.  
  266.     ; messages for installation procedure
  267. msg_inst1$ db    'install pushd: CED command list full', cr, lf, '$'
  268. msg_inst2$ db    'install popd: CED command list full', cr, lf, '$'
  269.  
  270.     ; command names
  271. pushd$    db    'pushd',cr
  272. popd$    db    'popd',cr
  273.  
  274. cseg    ends
  275.     end    main
  276.